<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Carbon;

/**
 * @property int $id
 * @property string $name
 * @property string $email
 * @property string $phone
 * @property string $password
 * @property string $avatar
 * @property string $role
 * @property int $status
 * @property Carbon $created_at
 * @property Carbon $updated_at
 * @property int $is_super
 */
class Admin extends Authenticatable
{
    protected $fillable = [
        'name',
        'email',
        'password',
        'avatar',
        'status',
        'is_super',
        'role',
        'last_login_at',
        'last_login_ip',
    ];

    protected $hidden = [
        'password',
        'remember_token',
    ];

    protected $casts = [
        'password' => 'hashed',
        'is_super' => 'boolean',
        'last_login_at' => 'datetime',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    public function isActive(): bool
    {
        return $this->status === 1;
    }

    public function isSuper(): bool
    {
        return $this->is_super === 1;
    }

    public function hasPermission($permission): bool
    {
        return $this->roles()->permissions()->where('permission', $permission)->exists();
    }

    public function roles(): BelongsTo
    {
        return $this->belongsTo(Role::class, 'role', 'slug');
    }

    public function getRoleNameAttribute(): string
    {
        return $this->roles->name;
    }
}
